package com.example.controller;

import com.example.anno.Log;
import com.example.pojo.Emp;
import com.example.pojo.PageBean;
import com.example.pojo.Result;
import com.example.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@RestController//方法返回值会以jsonv 和XML形式写入HTTP
@Slf4j
public class EmpController {
    @Autowired
    private EmpService empService;
    @GetMapping("/emps")
     public Result page(@RequestParam(defaultValue = "1") Integer page,
                        @RequestParam(defaultValue = "10") Integer pageSize,
                        String name, Short gender,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){

         log.info("page={},pageSize={},参数：{},{},{},{}",page,pageSize,name,gender,begin,end);
         PageBean pageBean= empService.page(page,pageSize,name,gender,begin,end );
         return Result.success(pageBean);

     }
    @Log
     @DeleteMapping("/emps/{ids}")
    public Result delete(@PathVariable("ids") Integer[] ids) {
         empService.delete(ids);
         return Result.success();
     }
    @Log
     @PostMapping("/emps")
    public Result save(@RequestBody Emp emp){
        log.info("新增员工，emp:{}",emp);
        empService.save(emp);
        return Result.success();
     }
     @GetMapping("/emps/{id}")
    public Result getById(@PathVariable("id") Integer id){
        log.info("id:{}",id);
        Emp emp= empService.getById(id);
         return Result.success(emp);
     }
     @Log
     @PutMapping("/emps")
    public Result update(@RequestBody Emp emp){
        log.info("修改员工，emp:{}",emp);
        empService.update(emp);
        return Result.success();
     }
}
